﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using DevExpress.Web.ASPxMenu;
using System.Xml;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxTreeList;

namespace SGDemo.Permission
{
    public partial class ButtonList : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
            JSManager.RegAjax(Header, this.GetType().BaseType);

            BindComBoBox();
        }

        /// <summary>
        /// 添加快捷方式
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveFastAdd()
        {
            string userkeyid = ApplicationContext.Current.UserKeyid;
            string pagecode = this.GetType().Name;
            PageEntity page = _permissionService.Page_GetKeyidBypagename(pagecode);
            if (page != null)
            {
                return _permissionService.Shortcuts_Add(userkeyid, page.Keyid);
            }
            else
            {
                return false;
            }
        }

        //模块的绑定下拉框
        public void BindComBoBox()
        {
            List<ModuleEntity> moduleList = _permissionService.Module_GetAll();
            foreach (ModuleEntity list in moduleList)
            {
                ListEditItem listbox = new ListEditItem();
                listbox.Text = list.ModuleName;
                listbox.Value = list.Keyid;
                cbModule.Items.Add(listbox);

            }

        }

        protected void tlPageList_CustomCallback(object sender, TreeListCustomCallbackEventArgs e)
        {
            var keyid = e.Argument;
          tlPageList.DataSource=  _permissionService.Page_GetByModuleKeyid(keyid);
          tlPageList.DataBind();
          tlPageList.ExpandToLevel(2);
        }

        /// <summary>
        /// 菜单项绑定事件 - 实现操作权限控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void menuHeader_ItemDataBound(object sender, MenuItemEventArgs e)
        {
            IHierarchyData itemHierarchyData = (IHierarchyData)e.Item.DataItem;
            XmlElement element = (XmlElement)itemHierarchyData.Item;
        }

        /// <summary>
        /// 自定义GridView回调重新绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            gvwList.PageIndex = 0;
            gvwList.DataBind();
            gvwList.Selection.UnselectAll();//不选中任何一行
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="keyids">Keyids集合</param>
        /// <returns>是否删除成功</returns>
        [AjaxPro.AjaxMethod]
        public bool BulkDelete(string keyids)
        {

            return _permissionService.Button_BulkDelete(keyids);
        }
    }

    public class ButtonManage
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        public List<ButtonEntity> PagingGetButton(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            return _permissionService.Button_PagingGet(startRecord, maxRecords, sortColumns, filterConditions);
        }

        public int PagingGetButtonCount(String filterConditions)
        {
            return _permissionService.Button_PagingGetCount(filterConditions);
        }
    }
}